import { chartEncaps } from './chartEncaps.js'

let drawz2 = obj => {
  // obj = {
  //   _t: this,
  //   doms: 'mycharts
  // }
  chartEncaps.initCanvas({ _t: obj._t, doms: obj.doms })
  if (obj._t.chartsConData.autoFit) {
    chartEncaps.setDisplay({ doms: obj.doms, display: 'block' })
  } else {
    chartEncaps.setDisplay({ doms: obj.doms, display: 'table' })
  }
  obj._t.chart.scale('value', {
    nice: true
  })
  var ds = new obj._t.$DataSet()
  let newData = chartEncaps.dealWithData({ _t: obj._t })
  var dv = ds.createView().source(newData)
  let field = []
  let key = ''
  let val = '值'
  switch (obj._t.chartsConData.ways) {
    case '1':
      field = ['媒体来源']
      key = 'value'
      break
    case '2':
    case '5':
      field = []
      for (let i in newData[0]) {
        if (i !== 'name' && i !== '_XID' && i !== 'count' && i !== 'value') {
          field.push(i)
        }
      }
      key = 'year'
      break
  }
  dv.transform({
    type: 'fold',
    fields: field, // 展开字段集
    key: key, // key字段
    value: val // value字段
  })
  obj._t.chart.tooltip({
    shared: true,
    showMarkers: false
  })
  chartEncaps.setLegend({ _t: obj._t, data: 'name' })
  obj._t.chart.data(dv.rows)
  chartEncaps.handleTickCount({
    _t: obj._t,
    data: { field: key, value: val }
  })
  obj._t.chart
    .interval()
    .position(`${key}*${val}`)
    .color('name', obj._t.chartsConData.themeColor)
    .adjust('stack')

  obj._t.chart.interaction('active-region')
  obj._t.chart.render()
  obj._t.setLoading(false)
  obj._t.setshadeDisplay(true)
}

export { drawz2 }
